﻿<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="ColorsAndBrushes.xaml" />
    </ResourceDictionary.MergedDictionaries>

    <!-- ScrollBar root template -->
    <ControlTemplate x:Key="ScrollBarTemplate"
                     TargetType="{x:Type ScrollBar}">
        <Grid x:Name="GridRoot"
              Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}"
              Background="{DynamicResource ScollbarBackground}">
            <Grid.RowDefinitions>
                <RowDefinition MaxHeight="18" />
                <RowDefinition Height="0.00001*" />
                <RowDefinition MaxHeight="18" />
            </Grid.RowDefinitions>

            <RepeatButton x:Name="DecreaseRepeat"
                          Command="ScrollBar.LineUpCommand"
                          Template="{DynamicResource ScrollRepeatButtonTemplate}" />

            <Track x:Name="PART_Track"
                   Grid.Row="1"
                   Orientation="Vertical"
                   IsDirectionReversed="True">
                <Track.Thumb>
                    <Thumb Template="{DynamicResource ScrollBarThumbTemplate}" />
                </Track.Thumb>
                <Track.IncreaseRepeatButton>
                    <RepeatButton x:Name="PageUp"
                                  Command="ScrollBar.PageDownCommand"
                                  Template="{DynamicResource TrackRepeatButtonTemplate}" />
                </Track.IncreaseRepeatButton>
                <Track.DecreaseRepeatButton>
                    <RepeatButton x:Name="PageDown"
                                  Command="ScrollBar.PageUpCommand"
                                  Template="{DynamicResource TrackRepeatButtonTemplate}" />
                </Track.DecreaseRepeatButton>
            </Track>

            <RepeatButton x:Name="IncreaseRepeat"
                          Grid.Row="2"
                          Command="ScrollBar.LineDownCommand"
                          Template="{DynamicResource ScrollRepeatButtonTemplate}" />
        </Grid>
        <ControlTemplate.Triggers>
            <!-- Sometimes the scroll needs to be horizontal, the layout needs to accomodate this-->
            <Trigger Property="Orientation"
                     Value="Horizontal">
                <Setter Property="LayoutTransform"
                        TargetName="GridRoot">
                    <Setter.Value>
                        <RotateTransform Angle="-90" />
                    </Setter.Value>
                </Setter>
                <Setter TargetName="PART_Track"
                        Property="Orientation"
                        Value="Vertical" />
                <Setter TargetName="DecreaseRepeat"
                        Property="Command"
                        Value="ScrollBar.LineLeftCommand" />
                <Setter TargetName="IncreaseRepeat"
                        Property="Command"
                        Value="ScrollBar.LineRightCommand" />
                <Setter TargetName="PageDown"
                        Property="Command"
                        Value="ScrollBar.PageLeftCommand" />
                <Setter TargetName="PageUp"
                        Property="Command"
                        Value="ScrollBar.PageRightCommand" />
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <!-- The thumb for adjustng the position on the ScrollBar -->
    <ControlTemplate x:Key="ScrollBarThumbTemplate"
                     TargetType="{x:Type Thumb}">
        <Grid Background="Transparent">
            <Border x:Name="Bd"
                    Margin="5 0 5 0"
                    Background="{DynamicResource NormalBrush}"
                    CornerRadius="4" />
        </Grid>
        <ControlTemplate.Triggers>
            <Trigger Property="IsMouseOver"
                     Value="True">
                <Setter TargetName="Bd"
                        Property="Background"
                        Value="{DynamicResource MouseOverBrush}" />
            </Trigger>
            <Trigger Property="IsDragging"
                     Value="True">
                <Setter TargetName="Bd"
                        Property="Background"
                        Value="{DynamicResource DraggingBrush}" />
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <!-- Nudge button at the top and bottom of the ScrollBar -->
    <ControlTemplate x:Key="ScrollRepeatButtonTemplate"
                     TargetType="{x:Type RepeatButton}">
        <Grid Background="Transparent">
            <Path x:Name="Arrow"
                  VerticalAlignment="Center"
                  HorizontalAlignment="Center"
                  Fill="{DynamicResource NormalBrush}"
                  Data="M0,0 L4,6 8,0z" />
        </Grid>
        <ControlTemplate.Triggers>
            <Trigger Property="IsMouseOver"
                     Value="True">
                <Setter TargetName="Arrow"
                        Property="Fill"
                        Value="{DynamicResource MouseOverBrush}" />
            </Trigger>
            <Trigger Property="IsPressed"
                     Value="True">
                <Setter TargetName="Arrow"
                        Property="Fill"
                        Value="{DynamicResource DraggingBrush}" />
            </Trigger>
            <Trigger Property="IsEnabled"
                     Value="False">
                <Setter TargetName="Arrow"
                        Property="Fill"
                        Value="{DynamicResource DisabledBrush}" />
            </Trigger>
            <Trigger Property="Name"
                     Value="DecreaseRepeat">
                <Setter TargetName="Arrow"
                        Property="LayoutTransform">
                    <Setter.Value>
                        <RotateTransform Angle="180" />
                    </Setter.Value>
                </Setter>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <!-- Area on the track that can be clicked to move the thumb -->
    <ControlTemplate x:Key="TrackRepeatButtonTemplate"
                     TargetType="{x:Type RepeatButton}">
        <Grid Margin="6,0,6,0" />
    </ControlTemplate>

</ResourceDictionary>